# -*- coding: UTF-8 -*-

import requests
import json
import urllib

def send_pic(pic):
    url = "http://api.heclouds.com/bindata"
    headers = {
        "Content-Type": "image/jpg", #
        "api-key": "gkojRvxMuALN1xMIg4V7=SnNgpg=", # 你的api-key
    }
    # device_id是你的设备id
    # datastream_id是你的数据流id

    querystring = {"device_id": "926899723", "datastream_id": "im"}
    # 流式上传

    with open('22.jpg', 'rb') as f:
        requests.post(url, params=querystring, headers=headers, data=f)
    print('success')


def post_json(id,json_path):
    with open(json_path,'r',encoding='utf-8') as f:
        value=json.load(f)
    url = "https://api.heclouds.com/devices/926899723/datapoints"
    streams = []
    #注意格式，上传的是json
    streams.append({"id": id, "datapoints": [{"value": value}]})
    values = {"datastreams": streams}
    data = json.dumps(values).encode("utf-8")
    request = urllib.request.Request(url, data)
    request.add_header('api-key', "gkojRvxMuALN1xMIg4V7=SnNgpg=")
    request.get_method = lambda: 'POST'
    request = urllib.request.urlopen(request)
    print(json.loads(request.read()))



